/*********************************************************************************
 *      Copyright:  (C) 2022 dairongan<2974932606@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  sysclass_ds18b20.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/14/2022)
 *         Author:  dairongan <2974932606@qq.com>
 *      ChangeLog:  1, Release initial version on "07/14/2022 10:35:26 AM"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <string.h>
#include "beep.h"


typedef u_int8_t uint8_t;

static int pwm_export(void)
{
        int fd;

		if(0!=access("/sys/class/pwm/pwmchip1/pwm0",F_OK))
		{
			fd = open("/sys/class/pwm/pwmchip1/export", O_WRONLY);
			if (fd < 0)
			{
					fprintf(stderr, "Failed to open export for writing!\n");
					return(-1);
			}


			if (write(fd,"0", 1) < 0)
			{
					fprintf(stderr, "Fail to export pwm!");
					return -1;
			}

			close(fd);
		}
        return 0;
}

static int pwm_unexport(void)
{
    int fd;

    fd = open("/sys/class/pwm/pwmchip1/unexport", O_WRONLY);
    if (fd < 0) {
        fprintf(stderr, "Failed to open unexport for writing!\n");
        return -1;
    }

    if (write(fd,"0", 1) < 0) {
        fprintf(stderr, "Fail to unexport pwm!");
        return -1;
    }

    close(fd);
    return 0;
}

static int pwm_enable(char *value)
{
    char path[DIRECTION_MAX];
    int fd;

    snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip1/pwm0/enable");
    fd = open(path, O_WRONLY);
    if (fd < 0) {
        fprintf(stderr, "failed to open pwm0/enable for writing!\n");
        return -1;
    }

    if (write(fd,value,1) < 0) {
        fprintf(stderr, "failed to set enable!\n");
        return -1;
    }

    close(fd);
    return 0;

}

/*static int pwm_disenable(void)
{
    char path[DIRECTION_MAX];
    int fd;

    snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip1/pwm0/enable");
    fd = open(path, O_WRONLY);
    if (fd < 0) {
        fprintf(stderr, "failed to open gpio direction for writing!\n");
        return -1;
    }

    if (write(fd,"0",1) < 0) {
        fprintf(stderr, "failed to set direction!\n");
        return -1;
    }

    close(fd);
    return 0;

}
*/
static int pwm_period_duty(void)
{
    char path[DIRECTION_MAX];
    int fd_p,fd_d;

    snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip1/pwm0/period");
    fd_p = open(path, O_WRONLY);
    if (fd_p < 0) {
        fprintf(stderr, "failed to open pwm period for writing!\n");
        return -1;
    }

	memset(path,0,DIRECTION_MAX);
    snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip1/pwm0/duty_cycle");
    fd_d = open(path, O_WRONLY);
    if (fd_d < 0) {
        fprintf(stderr, "failed to open pwm duty_cycley for writing!\n");
        return -1;
    }

    if (write(fd_p,"50000", 5) < 0) {
        fprintf(stderr, "failed to set period value!\n");
		close(fd_p);
        return -1;
    }

    if (write(fd_d,"10000", 5) < 0) {
        fprintf(stderr, "failed to set period value!\n");
		close(fd_d);
        return -1;
    }

    close(fd_p);
	close(fd_d);
    return 0;

}

int beep_open(void)
{	
	int rv=0;
	rv = pwm_export();
	if(0!=rv)
	{
		pwm_unexport();
		return rv;
	}

	rv = pwm_enable(PWM_ENABLE);
	if(0!=rv)
	{
		pwm_enable(PWM_DISENABLE);
		return rv;
	}

	rv = pwm_period_duty();
	if(0!=rv)
	{
		return rv;
	}

	return 0;
}

void beep_close(void)
{
	pwm_enable(PWM_DISENABLE);
	pwm_unexport();
}
